<include file="Public:header" />
<include file="Public:mainNavBar" />
<include file="Public:sidebar" />

<div id="wrapper" class="wat-cf">
     <div id="main">
		<div class="block" id="block-tables">
			<div class="content">
	            <h2 class="title">{:L('Users')}</h2>
	            <div class="inner">
	            <div style="display:blcok;">&nbsp;
						<div id="name_con_div" style="display:inline-block;">
							{:L('firstname')}:<input type="text" id="sfName" style="width:140px;height:28px;">	
							& {:L('lastname')}:<input type="text" id="slName" style="width:140px;height:28px;">
							& {:L('email')}:<input type="text" id="semail" style="width:140px;height:28px;">
							& start:<input id="startTime" readonly="readonly" type="text" style="width:150px;height:28px;"/>
							& end:<input id="endTime" readonly="readonly" type="text" style="width:150px;height:28px;" />
							& createdby
							<select id='createdby' style="height:28px;">
								<foreach name='agencyList' item='agency'>
									<option value="{$agency.name}">{$agency.name}</option>
								</foreach>
							</select>
							&nbsp;<button class="button" onclick="search();" style="float: right;">search</button>
						</div>
					
	             </div>   
	             <p></p>
	               <table id="dataTable" class="table">
	                  <tr>
	                    <th class="first"><input id="cbox1" type="checkbox" class="checkbox toggle" value='' /></th>
	                    <th>Index</th>
	                    <th>Uid</th>
	                    <th>Account</th>
	                    <th>Firstname</th>
	                    <th>Lastname</th>
	                    <th>Email</th>
	                    <th>Gender</th>
	                    <th>Paid</th>
	                    <th>Createdby</th>
	                    <th>Confirm</th>
	                    <th>Status</th>
	                    <th>Createdtime</th>
	                    <th></th>
	                    <th class="last">&nbsp;</th>
	                  </tr>
	                  <tbody id='listBody'>
	                  
	                  </tbody>
	                </table>
	                <div class="actions-bar wat-cf">
	                  <div class="actions">
	                    <button class="button" type="submit" onclick="delSelectedItems();">
	                      <img src="../Public/images/icons/cross.png" alt="Delete" />Delete
	                    </button>
	                  </div>
	                  <div id="pagination" class="pagination">
	                    <!-- <span class="disabled prev_page">« Previous</span><span class="current">1</span><a rel="next" href="#">2</a><a href="#">3</a><a href="#">4</a><a href="#">5</a><a href="#">6</a><a href="#">7</a><a href="#">8</a><a href="#">9</a><a href="#">10</a><a href="#">11</a><a rel="next" class="next_page" href="#">Next »</a> -->
	                  </div>
	                </div>
	            </div>
	          </div>
		</div>
		
		<include file="Public:userForm" />
		<include file="Public:message" />
	</div>
</div>
<script>
var currPageIndex = 1;
var totalPages = 0;
var sfName = '';
var slName = '';
var semail = '';
var startTime = '';
var endTime = '';
var createdby = '';

$(function(){
	loadPage(1);
	
	//search bar
	/*
	$('#cboxSearchName').attr('checked', 'true');
	$('#cboxSearchTime').removeAttr('checked');
	$('#cboxSearchName').click(function(){
		$('#cboxSearchTime').removeAttr('checked');
		$('#time_con_div').css('display', 'none');
		$('#name_con_div').css('display', 'inline-block');
	});
	$('#cboxSearchTime').click(function(){
		$('#cboxSearchName').removeAttr('checked');
		$('#time_con_div').css('display', 'inline-block');
		$('#name_con_div').css('display', 'none');
	});
	*/
	
	//search time
	$('#startTime').datepicker({
		'dateFormat' :'yy-mm-dd',
		changeYear: true,
		changeMonth: true,
		yearRange: "2013:2050"
	});
	$('#endTime').datepicker({
		'dateFormat' :'yy-mm-dd',
		changeYear: true,
		changeMonth: true,
		yearRange: "2013:2050"
	});
	
	
	$('#cbox1').removeAttr('checked');
	$('#cbox1').click(function(){
		if ($('#cbox1').attr('checked')) {
			$("input[type='checkbox']").attr('checked', 'true');			
		} else {
			$("input[type='checkbox']").removeAttr('checked');	
		}
	});
});

function loadPage(pageIndex) {
	currPageIndex = pageIndex;
	//load data
	$.post('/Admin/Users/loadUsersPage',{p:pageIndex,semail:semail,sfName:sfName,slName:slName,sTime:startTime,eTime:endTime,createdby:createdby},function(data){
		var obj = $.parseJSON(data);
		if (obj.status == 1) {
			loadData(obj);
		} else if(obj.status == 2) {
			$('#listBody').html('');
			showMessage(obj.msg, 'warning');
		} else if (obj.status == 0) {
			showMessage(obj.msg, 'error');
		}
	});
}

function loadData(obj) {
	var data = obj.data;
	var html = '';
	for(i=0; i<data.length; i++) {
		html = html + '<tr><td><input type="checkbox" class="checkbox" name="index" value="'+data[i].uid+'" /></td>';
		html = html + '<td>'+(i+1)+'</td>';
		html = html + '<td>'+data[i].uid+'</td>';
		html = html + '<td>'+data[i].account+'</td>';
		html = html + '<td>'+data[i].firstname+'</td>';
		html = html + '<td>'+data[i].lastname+'</td>';
		html = html + '<td>'+data[i].email+'</td>';
		
		var gender = (data[i].gender==1)?'Male':'Female';
		var paid = (data[i].paid==1)?'paid':'unpaid';
		var confirm = (data[i].confirmid ==1)?'confirmed':'unconfirmed';
		var status = (data[i].statusid==1)?'normal':'delete';
		var createdby = '';
		if (data[i].name == null || data[i].name == '') {
			createdby = 'user'
		} else {
			createdby = data[i].name
		}
		html = html + '<td>'+gender+'</td>';
		html = html + '<td>'+paid+'</td>';
		html = html + '<td>'+createdby+ '</td>';
		html = html + '<td>'+confirm+ '</td>';
		html = html + '<td>'+status+ '</td>';
		html = html + '<td>'+data[i].createtime+'</td>';
		html = html + '<td><a href="javascript:editItem('+data[i].uid+');" style="cursor:poniter;"><img src="../Public/images/icons/edit.png"/></a>&nbsp;&nbsp';
		html = html + '<a href="javascript:deleteItem('+data[i].uid+')" style="cursor:poniter;" ><img src="../Public/images/icons/trash.png"/></a></td></tr>';
	}
	$('#listBody').html(html);
	
	//biuld pagination
	var pagination = '';
	var page = obj.page;
	var nowPage = parseInt(page.nowPage);
	var prevPage = nowPage - 1;
	totalPages = page.totalPages;
	if (prevPage > 0) {
		pagination = '<span class="disabled prev_page" style="cursor:pointer;" onclick="javascript:loadPage('+prevPage+');">« Prev</span>';	
	} else {
		pagination = '<span class="disabled prev_page">« Prev</span>';
	}
	
	for (i=1; i<= page.totalPages; i++) {
		if (i == nowPage) {
			pagination = pagination + '<span class="current">'+i+'</span>';
		} else {
			pagination = pagination + '<a href="javascript:loadPage('+i+');">'+i+'</a>';					
		}
	}
	var nextPage = nowPage + 1;
	if (nextPage > page.totalPages) {
		pagination = pagination + '<span class="next_page">Next »</span>';	
	} else {
		pagination = pagination + '<span class="next_page" style="cursor:pointer;" onclick="javascript:loadPage('+nextPage+');">Next »</a></span>';
	}
	
	$('#pagination').html(pagination);
}

function deleteItem(id) {
	var ids = [];
	if (id != '') {
		if(confirm('是否要删除这条数据？')) {
			ids.push(id);
			doDelete(ids);		
		} else {
			return false;
		}
	}
}

function delSelectedItems() {
	var selectedItemIds = [];
	var tempId = '';
	$('input[type="checkbox"]').each(function(){
		tempId = $(this).val();
		if (tempId != '' && $(this).attr('checked')) {
			//selectedItemIds = selectedItemIds + tempId + ',';
			selectedItemIds.push(tempId);
		}
	});
	//selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
	if (selectedItemIds.length > 0) {
		if (confirm('是否要删除被选中的数据？')) {
			doDelete(selectedItemIds);
		} else {
			return false;
		}
	}
}
function doDelete(ids) {
	$.post('/Admin/Users/doDelete', {ids:ids}, function(data){
		var obj = $.parseJSON(data);
		if (obj.status == 1) {
			loadPage(currPageIndex);
		} else {
			alert(obj.msg);
		}
	});
}

function editItem(uid) {
	$.post('/Admin/Users/getUserInfo', {uid:uid}, function(data){
		var obj = $.parseJSON(data);
		if (obj.status == 1) {
			openEditDialog(obj.data);	
		}
	});
}

function search() {
	semail = $('#semail').val();
	sfName = $('#sfName').val();
	slName = $('#slName').val();
	startTime = $('#startTime').val();
	endTime = $('#endTime').val();
	createdby = $('#createdby').val();
	
	loadPage(1);
}
</script>
<include file="Public:footer" />
          